package com.pescamillam.kiosco.windows;

import javax.swing.*;

import java.awt.Font;
import java.awt.event.*;

//TODO mejorar interfaz para que se vea mejor, de pronto agregar logo de caracol 
public class Keyboard extends JFrame implements ActionListener {
	/** auto serial UID */
	private static final long serialVersionUID = -4512741336321564571L;
	private JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button0,buttonDel,buttonBack,buttonAccept;
	private JLabel document;
	private JLabel numbers;
	private String codeToSend;
	private static Keyboard instance;
	
	public static Keyboard getInstance() {
		if (instance == null) {
			instance = new Keyboard();
		}
		return instance;
	}
	
	private Keyboard() {
        setLayout(null);

        setContentPane(new JLabel(new ImageIcon("caracol-tv.png")));
        
        button1=new JButton("1");
        button1.setBounds(200,230,100,100);
        button1.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button1);
        button1.addActionListener(this);
        button1.setFocusable(false);
        
        button2=new JButton("2");
        button2.setBounds(320,230,100,100);
        button2.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button2);
        button2.addActionListener(this);
        button2.setFocusable(false);
        
        button3=new JButton("3");
        button3.setBounds(440,230,100,100);
        button3.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button3);
        button3.addActionListener(this);
        button3.setFocusable(false);
        
        button4=new JButton("4");
        button4.setBounds(200,350,100,100);
        button4.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button4);
        button4.addActionListener(this);
        button4.setFocusable(false);
        
        button5=new JButton("5");
        button5.setBounds(320,350,100,100);
        button5.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button5);
        button5.addActionListener(this);
        button5.setFocusable(false);
        
        button6=new JButton("6");
        button6.setBounds(440,350,100,100);
        button6.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button6);
        button6.addActionListener(this);
        button6.setFocusable(false);
        
        button7=new JButton("7");
        button7.setBounds(200,470,100,100);
        button7.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button7);
        button7.addActionListener(this);
        button7.setFocusable(false);
        
        button8=new JButton("8");
        button8.setBounds(320,470,100,100);
        button8.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button8);
        button8.addActionListener(this);
        button8.setFocusable(false);
        
        button9=new JButton("9");
        button9.setBounds(440,470,100,100);
        button9.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button9);
        button9.addActionListener(this);
        button9.setFocusable(false);
        
        button0=new JButton("0");
        button0.setBounds(320,590,100,100);
        button0.setFont(new Font("Arial", Font.PLAIN, 60));
        add(button0);
        button0.addActionListener(this);
        button0.setFocusable(false);
        
        buttonBack=new JButton("Borrar");
        buttonBack.setBounds(560,230,250,100);
        buttonBack.setFont(new Font("Arial", Font.PLAIN, 35));
        add(buttonBack);
        buttonBack.addActionListener(this);
        buttonBack.setFocusable(false);
        
        buttonDel=new JButton("Borrar todo");
        buttonDel.setBounds(560,350,250,100);
        buttonDel.setFont(new Font("Arial", Font.PLAIN, 35));
        add(buttonDel);
        buttonDel.addActionListener(this);
        buttonDel.setFocusable(false);
        
        buttonAccept=new JButton("Aceptar");
        buttonAccept.setBounds(560,470,250,100);
        buttonAccept.setFont(new Font("Arial", Font.PLAIN, 35));
        add(buttonAccept);
        buttonAccept.addActionListener(this);
        buttonAccept.setFocusable(false);
        
        document=new JLabel("Digite su documento");
        document.setBounds(300,50,500,100);
        document.setFont(new Font("Arial", Font.PLAIN, 50));
        add(document);

        numbers=new JLabel("",JLabel.CENTER);
        numbers.setBounds(300,150,400,60);
        numbers.setFont(new Font("Arial", Font.PLAIN, 50));
        add(numbers);
        
        codeToSend = "";
        
        setFocusable(true);
		setFocusTraversalKeysEnabled(false);
		
        addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				
				if (e.getKeyChar() == '\n'){
					ExitFrame.getInstance(codeToSend);
			    	codeToSend = "";
				} else {
					codeToSend+=e.getKeyChar();
				}
				e.consume();
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				e.consume();

			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				e.consume();
				
			}
		});
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==button1) {
        	numbers.setText(numbers.getText()+"1");
        }
        if (e.getSource()==button2) {
        	numbers.setText(numbers.getText()+"2");
        }
        if (e.getSource()==button3) {
        	numbers.setText(numbers.getText()+"3");
        }
        if (e.getSource()==button4) {
        	numbers.setText(numbers.getText()+"4");
        }  
        if (e.getSource()==button5) {
        	numbers.setText(numbers.getText()+"5");
        }  
        if (e.getSource()==button6) {
        	numbers.setText(numbers.getText()+"6");
        }  
        if (e.getSource()==button7) {
        	numbers.setText(numbers.getText()+"7");
        }  
        if (e.getSource()==button8) {
        	numbers.setText(numbers.getText()+"8");
        }  
        if (e.getSource()==button9) {
        	numbers.setText(numbers.getText()+"9");
        }  
        if (e.getSource()==button0) {
        	numbers.setText(numbers.getText()+"0");
        }  
        if (e.getSource()==buttonBack) {
        	if (numbers.getText().length() > 0) {
        		numbers.setText(numbers.getText().substring(0, numbers.getText().length()-1));
        	    }
        }  
        if (e.getSource()==buttonDel) {
        	numbers.setText("");
        }  
        if (e.getSource()==buttonAccept) {
        	openTakePictureFrame(numbers.getText());
        }
    }
	
    public void openTakePictureFrame(String id) {
    	
    	if (TakePictureFrame.getPersonName(id) != null) {
        	TakePictureFrame frame = TakePictureFrame.getInstance();
        	frame.identify(id);
        	frame.setVisible(true);
        	this.setVisible(false);
    	} else {
    		JOptionPane.showMessageDialog(null, "Documento " + id + " no encontrado");
    	}
    	
    }
    
    public static void main(String[] ar){
    	Keyboard keyboard1=new Keyboard();
    	keyboard1.setBounds(0,0,1024,800);
    	keyboard1.setExtendedState(JFrame.MAXIMIZED_BOTH);
    	keyboard1.setUndecorated(true);
    	
    	keyboard1.setVisible(true);
    }
    
    public void resetNumbers() {
    	numbers.setText("");
    }
}